/**
 * @desc 获取浏览器类型和版本
 * @return {Object}
 */
export function getBrowser() {
  let sys = {},
    ua = navigator.userAgent.toLowerCase(),
    s
  ;(s = ua.match(/rv:([\d.]+)\) like gecko/))
    ? (sys.ie = s[1])
    : (s = ua.match(/msie ([\d\.]+)/))
    ? (sys.ie = s[1])
    : (s = ua.match(/edg\/([\d\.]+)/))
    ? (sys.edg = s[1])
    : (s = ua.match(/edge\/([\d\.]+)/))
    ? (sys.edge = s[1])
    : (s = ua.match(/firefox\/([\d\.]+)/))
    ? (sys.firefox = s[1])
    : (s = ua.match(/(?:opera|opr).([\d\.]+)/))
    ? (sys.opera = s[1])
    : (s = ua.match(/chrome\/([\d\.]+)/))
    ? (sys.chrome = s[1])
    : (s = ua.match(/version\/([\d\.]+).*safari/))
    ? (sys.safari = s[1])
    : 0
  // 根据关系进行判断
  if (sys.ie) return { name: 'ie', version: sys.ie }
  if (sys.edg) return { name: 'edge', version: sys.edg }
  if (sys.edge) return { name: 'edge', version: sys.edge }
  if (sys.firefox) return { name: 'firefox', version: sys.firefox }
  if (sys.chrome) return { name: 'chrome', version: sys.chrome }
  if (sys.opera) return { name: 'opera', version: sys.opera }
  if (sys.safari) return { name: 'safari', version: sys.safari }
  return 'Unkonwn'
}

/**
 * @desc 获取操作系统类型
 * @return {Object}
 */
export function getOS() {
  let userAgent = ('navigator' in window && 'userAgent' in navigator && navigator.userAgent.toLowerCase()) || ''
  let vendor = ('navigator' in window && 'vendor' in navigator && navigator.vendor.toLowerCase()) || ''
  let appVersion = ('navigator' in window && 'appVersion' in navigator && navigator.appVersion.toLowerCase()) || ''

  if (/iphone/i.test(userAgent) || /ipad/i.test(userAgent) || /ipod/i.test(userAgent)) return { name: 'ios' }
  if (/android/i.test(userAgent)) return { name: 'android' }
  if (/win/i.test(appVersion) && /phone/i.test(userAgent)) return { name: 'windowsPhone' }
  if (/mac/i.test(appVersion)) return { name: 'macOSX' }
  if (/win/i.test(appVersion)) return { name: 'windows' }
  if (/linux/i.test(appVersion)) return { name: 'linux' }
}
